home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 21 / Mac Magazin and MacEasy Magazine CD - Issue 21.iso / Wissenschaft & Technik / yorick12vr1-nofpu folder / include / collec.i < prev    next >
Text File  |  1995-07-26  |  4KB  |  141 lines

  1. /*
  2.    COLLEC.I
  3.    Collect variables through all times in a time history file.
  4.  */
  5. /*    Copyright (c) 1994.  The Regents of the University of California.
  6.                     All rights reserved.  */
  7.  
  8. func raw_collect(f, name)
  9. /* xxDOCUMENT result= collect(f, name_string)
  10.      scans through all records of the history file F accumulating the
  11.      variable NAME_STRING into a single array with one additional
  12.      index varying from 1 to the number of records.
  13.  
  14.      NAME_STRING can be either a simple variable name, or a name
  15.      followed by up to four simple indices which are either nil, an
  16.      integer, or an index range with constant limits.  (Note that
  17.      0 or negative indices count from the end of a dimension.)
  18.  
  19.      Examples:
  20.         collect(f, "xle")        -- collects the variable f.xle
  21.     collect(f, "tr(2,2:)")   -- collects f.tr(2,2:)
  22.     collect(f, "akap(2,-1:0,)") -- collects f.akap(2,-1:0,)
  23.                  (i.e.- akap in the last two values of its
  24.                     second index)
  25.  */
  26. {
  27.   name= strtok(name, " \t(");
  28.   var= name(1);
  29.   name= name(2);
  30.  
  31.   /* this is yucky -- need a query function for current record number */
  32.   n0= where(strmatch(print(f),"Current record"));
  33.   n= 0;
  34.   if (numberof(n0)) {
  35.     n0= n0(1);
  36.     sread, print(f)(n0), n0,n, format=" Current record is number %ld of %ld";
  37.   } else {
  38.     error, "no record structure for file?";
  39.   }
  40.  
  41.   local a,b,c,d,e,name;
  42.  
  43.   if (!collect_get(a, name)) {
  44.     jr, f, 1;
  45.     rslt= array(get_member(f,var), n);
  46.     for (i=2 ; i<=n ; i++) {
  47.       jr, f, i;
  48.       rslt(.., i)= get_member(f,var);
  49.     }
  50.   } else if (!collect_get(b, name)) {
  51.     jr, f, 1;
  52.     rslt= array(get_member(f,var)(a), n);
  53.     for (i=2 ; i<=n ; i++) {
  54.       jr, f, i;
  55.       rslt(.., i)= get_member(f,var)(a);
  56.     }
  57.   } else if (!collect_get(c, name)) {
  58.     jr, f, 1;
  59.     rslt= array(get_member(f,var)(a, b), n);
  60.     for (i=2 ; i<=n ; i++) {
  61.       jr, f, i;
  62.       rslt(.., i)= get_member(f,var)(a, b);
  63.     }
  64.   } else if (!collect_get(d, name)) {
  65.     jr, f, 1;
  66.     rslt= array(get_member(f,var)(a, b, c), n);
  67.     for (i=2 ; i<=n ; i++) {
  68.       jr, f, i;
  69.       rslt(.., i)= get_member(f,var)(a, b, c);
  70.     }
  71.   } else if (!collect_get(e, name)) {
  72.     jr, f, 1;
  73.     rslt= array(get_member(f,var)(a, b, c, d), n);
  74.     for (i=2 ; i<=n ; i++) {
  75.       jr, f, i;
  76.       rslt(.., i)= get_member(f,var)(a, b, c, d);
  77.     }
  78.   } else {
  79.     error, "too many (>4) subscripts for collect";
  80.   }
  81.  
  82.   jr, f, n0;
  83.   return rslt;
  84. }
  85.  
  86. func collect_get(&ndx, &name)
  87. {
  88.   if (!name) return 0;
  89.  
  90.   argc= *pointer(name);
  91.   list= where(argc==',' | argc==')');
  92.   if (numberof(list)) {
  93.     list= list(1);
  94.     arg= list>1? strpart(name,1:list-1) : "";
  95.     name= list<strlen(name)? strpart(name,list+1:0) : string(0);
  96.   } else {
  97.     arg= name;
  98.     name= string(0);
  99.   }
  100.  
  101.   argc= *pointer(arg);
  102.   list= where(argc==':');
  103.   n= numberof(list);
  104.  
  105.   if (n==0) {
  106.     ndx= collect_num(arg);
  107.     return 1;
  108.  
  109.   } else {
  110.     l= list(1);
  111.     mn= l<2? [] : collect_num(strpart(arg, 1:l-1));
  112.     if (n==1) m= 1;
  113.     else m= list(2);
  114.     mx= l>=strlen(arg)? [] : collect_num(strpart(arg, l+1:m-1));
  115.     if (n==1) {
  116.       ndx= collect_rng(mn:mx);
  117.       return 2;
  118.     } else if (n==2) {
  119.       inc= m>=strlen(arg)? [] : collect_num(strpart(arg, m+1:0));
  120.       ndx= collect_rng(mn:mx:inc);
  121.       return 3;
  122.     }
  123.   }
  124.  
  125.   error, "index garbled or too complicated";
  126.   return 0;
  127. }
  128.  
  129. func collect_num(text)
  130. {
  131.   val= 0;
  132.   s= string(0);
  133.   n= sread(format="%ld%[^ \t\n]", text, val, s);
  134.   if (n==1) return val;
  135.   if (n==0 && !strtok(text," \t")(1)) return [];
  136.   error, "index too complicated-- not nil, number, or const range";
  137.   return string(0);
  138. }
  139.  
  140. func collect_rng(x) { return x; }
  141.